<?php

class Utility {

    public static function buildSuccessFailXmlResponse($success, $message, $content = '', $XMLHeaderContent = TRUE) {
        $dom = new DOMDocument('1.0');
        $response = $dom->appendChild($dom->createElement('response'));
        $successNode = $response->appendChild($dom->createElement('success'));
        $messageNode = $response->appendChild($dom->createElement('message'));
        if ($success) {
            $successNode->appendChild($dom->createTextNode(1));
        } else {
            $successNode->appendChild($dom->createTextNode(0));
        }
        $messageNode->appendChild($dom->createTextNode($message));
        if ($content) {
            $contentNode = $response->appendChild($dom->createElement('content'));
            if (is_array($content)) {
                foreach ($content as $key => $value) {
                    $temp = $contentNode->appendChild($dom->createElement($key));
                    if ($key == 'html') {
                        $temp->appendChild($dom->createTextNode('<![CDATA[' . $value . ']]>'));
                    } else {
                        $temp->appendChild($dom->createTextNode($value));
                    }
                }
            } else {
                $contentNode->appendChild($dom->createTextNode($content));
            }
        }
//generate xml
        $dom->formatOutput = true; // set the formatOutput attribute of
// domDocument to true
        $output = $dom->saveXML();
        if ($XMLHeaderContent) {
            header("Content-Type: text/xml; charset=utf-8");
        }

// save XML as string or file
        return $output; // send xml as a string to buffer
    }

    public static function getFileContent($filePath) {

        return implode("", file(self::getRealPath($filePath)));
    }

    public static function getRowColor() {
        static $rowColor = 'a';
        if ($rowColor == 'b') {
            $rowColor = 'a';
        } else {
            $rowColor = 'b';
        }

        return $rowColor;
    }

    public static function isAjaxRequest() {
        if ($_SERVER['X-Requested-With']) {
            $_SERVER['HTTP_X_REQUESTED_WITH'] = $_SERVER['X-Requested-With'];
        }
        if ($_SERVER['HTTP_X_REQUESTED_WITH']) {
            if (strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
                return true;
            }
        }
        return false;
    }

    public static function genRandomString($length) {
        $characters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        $string = "";

        for ($p = 0; $p < $length; $p++) {
            $string .= $characters[mt_rand(0, strlen($characters))];
        }

        return $string;
    }

    public static function getRealPath($path) {

        return $path;
        if (strstr(realpath($path), realpath(DIR_BASE))) {

            if (file_exists($path)) {
                return realpath($path);
            }
        } elseif (file_exists(realpath(DIR_BASE . $path))) {
            return realpath(DIR_BASE . $path);
        }
        throw new Exception('the file requested for inclusion - ' . $path . ' does not exist');
        return false;
    }

    public static function cleanURL ()
    {
        $temp = explode("?", $_SERVER["REQUEST_URI"]);
        $explodeArray = explode("/", $temp[0]);
        
        foreach ($explodeArray as $key => $item)
        {
            if (trim($item) == '')
            {
                unset($explodeArray[$key]);
            }
        }
        return implode("/", $explodeArray);
    
    }

    public static function getBasicURL ()
    {
        $explodedArray = explode("/", self::normalizeURL());
        return  $explodedArray[0];
    }

    public static function normalizeURL ()
    {
        $explodeArray = explode("/", self::cleanURL());
        
        return implode("/", $explodeArray);
    
    }
    

}

?>
